//
//  WP_ShowPhotoView.swift
//  WorkPlan
//
//  Created by 赵凯元 on 2019/6/11.
//  Copyright © 2019 YU. All rights reserved.
//

import UIKit

class WP_ShowPhotoView: UIView {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       self.addSubview(collectionView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    lazy var collectionView:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = kRatioWidth(10)
        layout.minimumInteritemSpacing = kRatioWidth(10)
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: (kScreenWidth - kRatioWidth(30)) / 2, height: (kScreenWidth - kRatioWidth(30)) / 2)
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: kRatioWidth(10), bottom: 0, right: kRatioWidth(10))
        //
        let collection = UICollectionView(frame: CGRect(x: 0, y: kRatioHeight(20), width: kScreenWidth, height: kRatioHeight(400)), collectionViewLayout: layout)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.white;
        collection.register(WP_PhotoCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        
        return collection
    }()
    lazy var imgArray:[UIImage] = {
        let arr = Array<UIImage>()
        return arr
    }()

    func reloadData(arr:Array<UIImage>) {
        imgArray = arr
        collectionView.reloadData()
    }
 
}

extension WP_ShowPhotoView :UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell:WP_PhotoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! WP_PhotoCollectionViewCell
        
        cell.img.image = imgArray[indexPath.row]
        
        return cell
    }
    
    
    
    
}
